package com.tarim.streetdance.api;

import com.alibaba.fastjson.JSONObject;
import com.tarim.streetdance.model.User;
import com.tarim.streetdance.model.VideoViews;
import com.tarim.streetdance.service.VideoViewsService;
import com.tarim.streetdance.util.Constants;
import com.tarim.streetdance.util.JsonResult;
import com.tarim.streetdance.util.RedisUtil;
import com.tarim.streetdance.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@CrossOrigin
@RequestMapping("/api/videoViews")
public class VideoViewsApi {
    @Autowired
    private VideoViewsService videoViewsService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 新增观看记录
     * @param videoViews
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/insert")
    public JsonResult insertSelective (VideoViews videoViews, HttpServletRequest request, HttpServletResponse response) {
        JsonResult jsonResult;
        try {
            //用户登录后会把用户信息存到redis里
            //获取用户信息
            Object object = redisUtil.get(request.getHeader("token"));
            if (object == null) {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "登录后才可新增观看记录");
                return jsonResult;
            }
            //把redis存的数据转换成项目中的User实体类
            JSONObject jsonObject = (JSONObject) object;
            User user = jsonObject.toJavaObject(User.class);

            //获取到用户信息之后，存到观看记录实体类里
            videoViews.setUserId(user.getId());

            //把观看记录插入到数据库中
            videoViewsService.insertSelective(videoViews);

            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "成功");
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "系统异常,请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 删除观看记录
     * @param videoViews
     * @return
     */
    @RequestMapping("/delete")
    public JsonResult updateForDelete (VideoViews videoViews) {
        JsonResult jsonResult;
        try {
            videoViewsService.updateForDelete(videoViews);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "删除成功");
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "系统异常,请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 查看用户观看记录
     * @param pageIndex
     * @param pageSize
     * @param userId
     * @return
     */
    @RequestMapping("/selectByUser")
    public JsonResult selectByUserId (@RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(value = "userId") Long userId) {
        JsonResult jsonResult;
        try {
            PageBean<VideoViews> pageBean = new PageBean<>(pageIndex, pageSize);
            VideoViews videoViews = new VideoViews();
            videoViews.setUserId(userId);
            pageBean = videoViewsService.selectByUserId(pageBean, videoViews);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "成功", pageBean);
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "系统异常,请联系管理员");
        }
        return jsonResult;
    }
}
